@tool
class_name Condition_Expression_EntityData
extends Condition_Expression

## 表达式内使用直接用"value"访问属性即可

@export var attribute_name:String

# 检查结果
func check_result(entity:Entity) -> bool:
	if not super(entity):
		return false
	if not is_instance_valid(entity):
		push_error("找不到Entity，无法求表达式的值")
		return false
	var result = evaluate_expression(entity,expression,false)
	if typeof(result) != TYPE_BOOL:
		push_error("表达式{0}结果为{1}，不是bool，返回false".format([expression,result]))
		return false
	return result

func evaluate_expression(entity:Entity,expression:String,default_value:Variant) -> Variant:
	var ex = Expression.new()
	expression_properties["entity"] = entity
	expression_properties["value"] = get_property()
	var input_names = expression_properties.keys()
	var result = ex.parse(expression,input_names)
	if result != OK:
		push_error("解析{0}错误：{1}".format([expression,ex.get_error_text()]))
		return default_value
	var input_values:Array = []
	for input_name in input_names:
		input_values.append(expression_properties[input_name])
	var excute_result = ex.execute(input_values,entity,false)
	if ex.has_execute_failed():
		push_error("执行{0}错误：{1}".format([expression,ex.get_error_text()]))
		return default_value
	return excute_result
func get_property():
	return entity.entity_data.get_attribute_value(attribute_name)
